package com.cris.controller;

import com.cris.helper.MapHelper;
import com.cris.model.ProductType;
import com.cris.service.ProductTypeService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by owen on 2017/4/7.
 */
@Controller
public class ProductTypeController {
    private static final Log logger = LogFactory.getLog(ProductTypeController.class);

    @Autowired
    private ProductTypeService productTypeService;
    //查看产品类型
    @RequestMapping(value = "/productType",method = RequestMethod.GET)
    public String productTypeShow(Model model){
        List<ProductType> productTypeListParent = productTypeService.getParentProductTypeList();

        model.addAttribute("productTypeListParent",productTypeListParent);
        return "productType";
    }
    //进入创建产品类型界面
    @RequestMapping(value = "/productType_create",method = RequestMethod.GET)
    public String showCreate(Model model){
        List<ProductType> productTypeList=productTypeService.getProductTypeList();
        model.addAttribute("productTypeList",productTypeList);
        model.addAttribute("productType",new ProductType());
        return "productType_create";
    }
    //进入创建子产品类型界面
    @RequestMapping(value = "/productType_create_child",method = RequestMethod.GET)
    public String showCreateChild(Model model){
        List<ProductType> productTypeList=productTypeService.getParentProductTypeList();
        model.addAttribute("productTypeList",productTypeList);
        model.addAttribute("productType",new ProductType());
        return "productType_create_child";
    }

    //创建产品大类型
    @RequestMapping(value="/productType_create",method = RequestMethod.POST)
    public String doCreate(@ModelAttribute ProductType productType){
        Map<String,Object> map;
        map=MapHelper.put(productType);
        productTypeService.addProductType(map);
        return "redirect:/productType";
    }
    //创建子产品类型
    @RequestMapping(value="/productType_create_child",method = RequestMethod.POST)
    public String doCreateChild(@ModelAttribute ProductType productType){
        Map<String,Object> map;
        map=MapHelper.put(productType);
        productTypeService.addProductType(map);
        return "redirect:/productType";
    }

    //删除产品类型
    @RequestMapping(value = "/productType_delete",method = RequestMethod.GET)
    public String deleteProduct(@RequestParam long id, Model model){
        productTypeService.deleteProductType(id);
        return "redirect:/productType";
    }
    //进入编辑产品类型界面
    @RequestMapping(value = "/productType_edit",method = RequestMethod.GET)
    public String showEdit(@RequestParam long id,Model model){
        ProductType productType = productTypeService.getProductType(id);
        if (productType.getParentid()!=null) {
            List<ProductType> productTypeList = productTypeService.getParentProductTypeList();
            model.addAttribute("productTypeList",productTypeList);
        }
        model.addAttribute("productType",productType);
        return "productType_edit";
    }
    //编辑产品类型
    @RequestMapping(value = "/productType_edit",method = RequestMethod.POST)
    public String doEdit(@ModelAttribute ProductType productType,@RequestParam long id){
        HashMap<String,Object> map;
        map=MapHelper.put(productType);
        productTypeService.updateProductType(id,map);
        return "redirect:/productType";

    }
    //按照大类显示小类
    @RequestMapping(value = "/productType_showByParent",method = RequestMethod.GET)
    public String showChildType(Model model,@RequestParam long id){

        List<ProductType> productTypeListChild =productTypeService.getChildProductTypeByParentTypeId(id);
        List<ProductType> productTypeList = productTypeService.getParentProductTypeList();
        model.addAttribute("productTypeList",productTypeList);
        model.addAttribute("productTypeListChild",productTypeListChild);
        return "productType_show";
    }


}
